package site.zhoulei.springbootstu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import site.zhoulei.springbootstu.model.AjaxResponse;
import site.zhoulei.springbootstu.model.Article;
import site.zhoulei.springbootstu.service.ArticelRestService;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author zlleige
 * @create 2020-07-29 14:28
 */
@Slf4j //lombok日志
@RestController //@RestController相当于 @Controller和@ResponseBody结合。一是作为Controller的作用，将控制器类注入到Spring上下文环境，该类RequestMapping标注方法为HTTP服务端点。        二是作为ResponseBody的作用，请求响应默认使用的序列化方式是JSON，而不是跳转到jsp或模板页面。
@RequestMapping("rest") //用于标注HTTP服务端点
public class ArticleController {

    //注入一个ArticleRestService 对象, 用于测试ArticleRestControllerTest3方案，mock测试，不用直接执行saveArticle方法
    @Resource
    ArticelRestService articelRestService;


    //获取一篇Article,使用Get方法，根据id查一篇文章
    //@RequestMapping(value="/articles/{id}",method = RequestMethod.GET,produces = "application/json")
    @GetMapping("/articles/{id}")
    public @ResponseBody AjaxResponse getArticle(@PathVariable("id") Long id){
        //使用lombok提供的builder
        Article article = Article.builder().id(id).author("zl").content("学习springboot").createTime(new Date()).title("t1").build();
        log.info("getArticle: " + article);
        return AjaxResponse.success(article);
    }


    //增加一篇文章
    //@RequestMapping(value = "/articles",method = RequestMethod.POST) 可由以下注释替换
    @PostMapping("/articles")
    //public @ResponseBody AjaxResponse saveArticle(@RequestBody Article article,@RequestHeader String aaa){
    public @ResponseBody AjaxResponse saveArticle(@RequestBody Article article){
        //测试接口
        log.info("saveArticle: "+article);

        log.info("articleRestService return : "+articelRestService.saveArticle(article));
        return AjaxResponse.success(article);
    }

    //增加一篇Article ，使用POST方法(RequestParam方式接收参数)
    /*@PostMapping("/articles")
    public AjaxResponse saveArticle(@RequestParam  String author,
                                  @RequestParam  String title,
                                  @RequestParam  String content,
                                  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                  @RequestParam  Date createTime){

    log.info("saveArticle:" + createTime);
    return AjaxResponse.success();
    }*/


    //修改更细文章，使用PUT方法，以id主键更新 ResponseBody采用json格式返回
    //@RequestMapping(value = "/articles/{id}",method = RequestMethod.PUT)
    //public @ResponseBody AjaxResponse updateArticle(@RequestBody Article article){
    @PutMapping("/articles/{id}")
    public @ResponseBody AjaxResponse updateArticle(@PathVariable("id") Long id,@RequestBody Article article){
        article.setId(id);
        if(article.getId() == null){
            //article的id必须传，通常根据id更新参数
            //todo 抛出自定义异常
        }

        log.info("updateArticle: " + article);
        return AjaxResponse.success(article);
    }

    //@RequestMapping(value = "/articles/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/articles/{id}")
    public @ResponseBody AjaxResponse deleteArticle(@PathVariable("id") Long id){
        //操作删除数据
        log.info("deleteArticle: " + id);
        return AjaxResponse.success(id);
    }
}
